package com.senken.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;

import com.senken.commons.result.Result;



public class UploadFile {
	
	
	
	public static Result UpLoad(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Result result = new Result();
		request.setCharacterEncoding("utf-8");  
        response.setCharacterEncoding("utf-8");  
        //1、创建一个DiskFileItemFactory工厂  
        DiskFileItemFactory factory = new DiskFileItemFactory();  
        //2、创建一个文件上传解析器  
        ServletFileUpload upload = new ServletFileUpload(factory);  
        //解决上传文件名的中文乱码  
        upload.setHeaderEncoding("UTF-8");   
        factory.setSizeThreshold(1024 * 500);//设置内存的临界值为500K  
        File linshi = new File("D:\\");//当超过500K的时候，存到一个临时文件夹中  
        factory.setRepository(linshi);  
        upload.setSizeMax(1024 * 1024 * 5);//设置上传的文件总的大小不能超过5M  
        try {  
            // 1. 得到 FileItem 的集合 items  
            List<FileItem> /* FileItem */items = upload.parseRequest(request);  
            System.out.println(items);
            // 2. 遍历 items:  
            for (FileItem item : items) {  
                // 若是一个一般的表单域, 打印信息  
                if (item.isFormField()) {  
                    String name = item.getFieldName();  
                    String value = item.getString("utf-8");  
  
                    System.out.println(name + ": " + value);  
                      
                      
                }  
                // 若是文件域则把文件保存到 e:\\files 目录下.  
                else {
                	String[] reg = {"jpg","png","JPG","PNG","doc","docx","txt"};
                    String fileName = item.getName();
                    long sizeInBytes = item.getSize(); 
                    String prefix=fileName.substring(fileName.lastIndexOf(".")+1);//获取文件后缀名
                    String name = fileName.substring(0, fileName.lastIndexOf("."));
                    fileName = name+"_"+System.currentTimeMillis()+"."+prefix;
                    boolean index =false;
                    for (int i = 0; i < reg.length; i++) {
						if(prefix.equals(reg[i])){
							InputStream in = item.getInputStream();  
		                    byte[] buffer = new byte[1024];  
		                    int len = 0;  
		                    String url = "/d/uploadfile/"+fileName;
		                    String SavePath = "D:/uploadfile/" +fileName;//文件最终上传的位置    
		                    OutputStream out = new FileOutputStream(SavePath);  
		  
		                    while ((len = in.read(buffer)) != -1) {  
		                        out.write(buffer, 0, len);  
		                    }  
		                    
		                    out.close();  
		                    in.close();
		                    index= true;
		                    result.setObj(url);
		                    result.setSuccess(true);
		                    return result;
						}						
					}
                     
                }  
            }  
  
        } catch (FileUploadException e) {        	
            e.printStackTrace(); 
            return result;
        }  
        return result;
	}
}
